package com.suray.wcs.service.init;

import com.suray.basic.wcs.path.core.Node;
import com.suray.basic.wcs.plc.pojo.lifter.data.map.CarMap;
import com.suray.basic.wcs.plc.pojo.lifter.data.map.LifterMap;
import com.suray.basic.wcs.plc.pojo.lifter.data.map.PalletMap;
import com.suray.commin.core.utils.SpringContextHolder;
import com.suray.basic.wcs.plc.entity.LifterInfoDB;
import com.suray.basic.wcs.plc.service.impl.LifterInfoDBServiceImpl;
import com.suray.basic.wcs.plc.utils.LifterInfoType;
import com.suray.basic.wcs.plc.utils.LifterLocationType;
import com.suray.basic.wcs.plc.utils.PalletStatus;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class LifterInfoFunc {

    private static final LifterInfoDBServiceImpl lifterInfoDBService
            = SpringContextHolder.getBean(LifterInfoDBServiceImpl.class);

    /**
     * 获取所有提升机数据
     * @param series
     * @return
     */
    public List<LifterInfoDB> getAllLifterInfos(String series) {
        return lifterInfoDBService.getAllLifterInfos(series);
    }

    /**
     * 插入提升机层数据
     * @param lifterMap
     * @param series
     */
    public void saveLifterInfo(LifterMap lifterMap, String series) {
        LifterInfoDB lifterInfoDB = new LifterInfoDB();
        lifterInfoDB.setSeries(series);
        lifterInfoDB.setType(LifterInfoType.LIFTER_LAYER.getValue());
        lifterInfoDB.setLocation(String.valueOf(lifterMap.layer));
        lifterInfoDB.setDistance(lifterMap.distance);
        lifterInfoDBService.saveOrUpdate(lifterInfoDB);
    }

    /**
     * 删除提升机位移数据
     * @param series
     */
    public void deleteLifterDisData(String series) {
        lifterInfoDBService.deleteLifterDisInfoBySeries(series, LifterInfoType.LIFTER_LAYER.getValue());
    }

    /**
     * 获取提升机层数据
     * @param series
     * @return
     */
    public List<LifterMap> getLifterInfos(String series) {
        List<LifterMap> lifterMaps = new ArrayList<>();
        List<LifterInfoDB> lifterInfoDBS = lifterInfoDBService.getLifterInfos(series);
        lifterInfoDBS.forEach(lifterInfoDB -> lifterMaps.add(new LifterMap(Integer.valueOf(lifterInfoDB.getLocation()), lifterInfoDB.getDistance())));
        return lifterMaps;
    }

    /**
     * 更新提升机层位移量
     * @param lifterMap
     * @param series
     * @return
     */
    public boolean updateLifterInfo(LifterMap lifterMap, String series) {
        return lifterInfoDBService.updateLifterInfoByLayer(lifterMap.layer, lifterMap.distance, series);
    }

    /**
     * 插入小车数据
     * @param carMap
     * @param series
     */
    public void saveCarInfo(CarMap carMap, String series) {
        LifterInfoDB lifterInfoDB = new LifterInfoDB();
        lifterInfoDB.setSeries(series);
        lifterInfoDB.setType(LifterInfoType.TWO_DIR_CAR.getValue());
        lifterInfoDB.setLocation(carMap.node + "_" + carMap.lifterLocationType.getDes());
        lifterInfoDB.setDistance(carMap.distance);
        lifterInfoDB.setGroundCode(carMap.groundCode);
        lifterInfoDBService.saveOrUpdate(lifterInfoDB);
    }

    /**
     * 获取两向车数据
     * @param series
     * @return
     */
    public List<CarMap> getCarInfos(String series) {
        List<CarMap> carMaps = new ArrayList<>();
        lifterInfoDBService.getCarInfos(series).forEach(lifterInfoDB -> {
            String[] strs = lifterInfoDB.getLocation().split("_");
            Node node = Node.parseGrid(strs[0]);
            LifterLocationType locationType = LifterLocationType.parseLocal(strs[1]);
            carMaps.add(new CarMap(node, locationType, lifterInfoDB.getGroundCode(), lifterInfoDB.getDistance()));
        });
        return carMaps;
    }

    /**
     * 更新两向车位移量
     * @param carMap
     * @param series
     * @return
     */
    public boolean updateCarInfo(CarMap carMap, String series) {
        return lifterInfoDBService.updateCarInfoByLayer(carMap.node, carMap.lifterLocationType,
                carMap.groundCode, carMap.distance, series);
    }

    /**
     * 插入托盘数据
     * @param palletMap
     * @param series
     */
    public void savePalletInfo(PalletMap palletMap, String series) {
        LifterInfoDB lifterInfoDB = new LifterInfoDB();
        lifterInfoDB.setSeries(series);
        lifterInfoDB.setType(LifterInfoType.CAR_PALLET.getValue());
        lifterInfoDB.setLocation(String.valueOf(palletMap.palletStatus.getStatusNum()));
        lifterInfoDB.setDistance(palletMap.distance);
        lifterInfoDBService.saveOrUpdate(lifterInfoDB);
    }

    /**
     * 获取托盘数据
     * @param series
     * @return
     */
    public List<PalletMap> getPalletInfos(String series) {
        List<PalletMap> palletMaps = new ArrayList<>();
        lifterInfoDBService.getPalletInfos(series).forEach(lifterInfoDB -> palletMaps.add(new PalletMap(PalletStatus.parseStatus(Integer.valueOf(lifterInfoDB.getLocation())),
                lifterInfoDB.getDistance())));
        return palletMaps;
    }

    /**
     * 更新托盘位移量
     * @param palletMap
     * @param series
     * @return
     */
    public boolean updatePalletInfo(PalletMap palletMap, String series) {
        return lifterInfoDBService.updatePalletInfoByLayer(palletMap.palletStatus, palletMap.distance, series);
    }

    /**
     * 删除提升机数据
     * @param series
     */
    public void destroyLifter(String series) {
        lifterInfoDBService.deleteLifterInfoBySeries(series);
    }

    public static void clearLifterData(String series) {
        lifterInfoDBService.deleteLifterInfoBySeries(series);
    }
}
